"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DBV认证兼容性
Case Name   : 版本号校验
Create At   : 2023/3/3
Owner       : @wan005
Description :
    1.编译并运行java文件
    2.检查版本号是否是openGauss LTS及补丁版本号
Expect      :
    1.编译并运行java文件成功，得到执行结果
    2.版本号是openGauss LTS及补丁版本号
History     :
"""
import os
import re
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class JdbcCertify(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.db_primary_user_node = Node(node='PrimaryDbUser')
        self.db_primary_root_node = Node(node='PrimaryRoot')
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.jdbc_path = os.path.join(path, 'dir_certification_0002')
        self.java_name = "jdbc_Certification_Case0002"
        self.common = Common()
        self.constant = Constant()

    def test_index(self):
        text = '-----1.编译并运行java文件-----'
        self.log.info(text)
        conf_list = {"password":self.db_primary_user_node.db_password,
                     "port":self.db_primary_user_node.db_port,
                     "hostname":self.db_primary_user_node.db_host,
                     "user":self.db_primary_user_node.db_user,
                     "dbname":self.db_primary_user_node.db_name,
                     "stringtype":"unspecified"}
        result_java = self.common.execute_jdbc(conf_list, self.jdbc_path, 
                    self.java_name, self.db_primary_root_node)

        self.assertNotIn('ERROR', result_java, "执行失败:" + text)
        self.assertEqual(1, result_java.count("版本号："), "执行失败:" + text)
        for i in result_java.splitlines():
            if "版本号" in i:
                ver_str = i.split("号：")[1].strip()
                self.log.info(ver_str)

        text = '-----2.检查版本号是否正确-----'
        self.log.info(text)
        ver_path = os.path.join(macro.SCRIPTS_PATH, 'openGauss_versionlist.txt')
        with open(ver_path) as file:
            version_list = file.readlines()
        self.log.info(version_list)
        find_flag = False
        for each_ver in version_list:
            if each_ver.strip() in ver_str:
                find_flag = True
                break
        self.assertTrue(find_flag, "认证版本号不在openGauss LTS列表中，执行失败:" + text)


    def tearDown(self):
        text = '-----this is tearDown-----'
        self.log.info(text)
        cmd = f"rm -rf {self.jdbc_path} && ls {self.jdbc_path}"
        self.log.info(cmd)
        msg = self.db_primary_root_node.sh(cmd).result()
        self.assertIn(self.constant.no_file_msg, msg, "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')